package com.augurit.common.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.AcRole;
import com.augurit.common.system.convert.AcRoleConverter;
import com.augurit.common.system.web.org.form.AcRoleForm;
import com.augurit.common.util.HqlUtils;

/**
 * 角色DAO类
 */
@Repository
public class AcRoleDao extends BaseDao<AcRole, Long> {

	@Autowired
	private AcPartyRoleDao acPartyRoleDao;
	
	public AcRoleForm getForm(Long id){
		AcRoleForm form = null;
		
		if(id != null){
			AcRole entity = this.get(id);
			if(entity != null)
				form = AcRoleConverter.convertToForm(entity);
		}
		
		return form;
	}
	
	/**
	 * 保存应用系统Form对象
	 * @param form 应用系统Form对象
	 */
	public void save(AcRoleForm form){
		
		if(form != null){
			AcRole entity = null;
			
			//此处为处理Ext中Ext.ux.grid.EditorGrid会自动把隐含字段的值置0
			if(form.getRoleId() == null || form.getRoleId() == 0)
				form.setRoleId(null);
			
			//准备VO对象
			if(form != null && form.getRoleId() != null ){
				entity = this.get(form.getRoleId());
			}else{
				entity = new AcRole();
			}
			
			//属性值转换
			AcRoleConverter.convertToEntity(form, entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setRoleId(entity.getRoleId());
		}
	}

	/**
	 * 获取指定岗位的关联角色id列表
	 * @param posId 岗位id
	 * @return 角色form对象列表
	 */
	public List<AcRoleForm> getRolesByPosId(Long posId) {
		return getRoles(OrgConstant.PARTY_TYPE_POSITION, posId);
	}
	
	/**
	 * 获取与组织对象关联的角色form对象集合
	 * @param partyType 组织对象类型
	 * @param partyId  组织对象主键ID
	 * @return 角色form对象列表
	 */
	@Transactional(readOnly = true)
	public List<AcRoleForm> getRoles(String partyType, Long partyId) {
		// 获取关联的角色ID集合
		Long[] roleIds = acPartyRoleDao.getRoleIds(partyType, partyId);

		// 获取角色对象
		List<AcRole> list = this.getRoles(roleIds);
		return AcRoleConverter.convertToFormList(list);
	}
	
	/**
	 * 获取所有的角色
	 * @return
	 */
	public List<AcRoleForm> getAllRoles(){
		List<AcRole> list = this.getAll();
		return AcRoleConverter.convertToFormList(list);
	}
	
	/**
	 * 根据角色ID集合获取Entity对象列表
	 * @param roleIds
	 * @return
	 */
	public List<AcRole> getRoles(Long[] roleIds){
		return partSelectByRoleId(roleIds, null);
	}
	
	/**
	 * 根据角色ID集合获取主键id列表
	 * @param roleIds
	 * @return
	 */
	public List<Long> getRoleIds(Long[] roleIds){
		if(roleIds != null && roleIds.length > 0){
			return partSelectByRoleId(roleIds,"roleId");
		}
		return null;
	}
	
	private List partSelectByRoleId(Long[] roleIds, String entityProperty){
		if(roleIds != null && roleIds.length > 0){
			String hql = "select "
			             + (entityProperty != null && entityProperty.trim().length() > 0 ? "ps." + entityProperty : "ps")
			             + " from AcRole ps where " 
			             + HqlUtils.buildOrCondition("ps", "roleId", roleIds);      
			return this.find(hql);
		}else
			return null;
	}
	
	/**
	 * 根据工作组id删除关联角色
	 */
	public void disAssociationRoleFromGroup(Long groupId) {
		if (groupId != null) {
			List<AcRoleForm> list = this.getRoles(OrgConstant.PARTY_TYPE_GROUP, groupId);
			if (list != null && list.size() > 0) {
				for (AcRoleForm form : list)
					acPartyRoleDao.disAssociateRoleFromGroup(form.getRoleId(),groupId);
			}
		}
	}
	
	/**
	 * 根据机构ID删除关联角色
	 */
	public void disAssociationRoleByOrgId(Long orgId) {
		if (orgId != null) {
			List<AcRoleForm> list = this.getRoles(OrgConstant.PARTY_TYPE_ORG, orgId);
			if (list != null && list.size() > 0) {
				for (AcRoleForm form : list)
					acPartyRoleDao.disAssociateRoleFromOrg(form.getRoleId(), orgId);
			}
		}
	}
	
	//判断角色表中是否有匹配的记录
	/*public boolean hasRoleLikeKeyword(String keyword){
		if(keyword != null && keyword.trim().length()>0){
			String hql ="from AcRole ps where ps.roleName like '" +keyword +"%'";
			List<AcRole> list = this.find(hql);
			if(list != null && list.size()>0){
				return true;
			}
		}
		return false;
	}*/
}
